﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.pro2e.console.core.hibernate;
using NHibernate;

using com.pro2e.console.service.auth;
using com.pro2e.console.service.auth.vo;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Generic;
using com.pro2e.web.util;
using com.pro2e.console.service.log.vo;

/// <summary>
/// AuthModule 的摘要描述
/// </summary>
/// 
namespace com.pro2e.web.auth
{
    public class LogModule : IHttpModule, IRequiresSessionState
    {
        log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        
        public LogModule()
        {
            
        }
        #region Fields and Properties

        #endregion

        #region IHttpModule Members

        public void Init(HttpApplication application)
        {
            application.AcquireRequestState += new EventHandler(FillLog);
            
        }

        public void Dispose() { }

        #endregion

        public void FillLog(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;
            
            string rawUrl = application.Request.RawUrl;

            rawUrl = rawUrl.ToLower();

            //只欄aspx
            if (rawUrl.IndexOf(".aspx") == -1)
            {
                return;
            }

            if (rawUrl.IndexOf("schema.aspx") != -1) 
            {
                return;
            }


            //將function Name存入log                        

            if (!string.IsNullOrEmpty(rawUrl))
            {
                string path = rawUrl;

                string phypath = application.Request.ApplicationPath.ToLower();

                if (!string.IsNullOrEmpty(phypath)&& !phypath.Equals("/")) 
                {
                    path = path.Replace(phypath, "");
                }

                if (path.StartsWith("/")) 
                {
                    path = path.Substring(1);
                }


                AuthService authService =new AuthService();

                IList<MenuFunc> menuFuncList = authService.DaoGetByWhere<MenuFunc>("as vo where vo.MainPath='"+path+"'", 0, 1);
               
                //checkMainPath
                if (menuFuncList != null && menuFuncList.Count > 0)
                {
                    FillLog(menuFuncList[0]);
                }

                //check functionPath                
                IList<FunctionPath> fpaths = authService.DaoGetByWhere<FunctionPath>("as vo where vo.Path='" + path + "'", 0, 1);
                if (fpaths != null && fpaths.Count > 0)
                {
                    FillLog(fpaths[0].BelongMenuFunc);
                }
                

            }                      
        }

        private void FillLog(MenuFunc m)
        {
            string functionName = m.ParentMenu.MenuFuncName;
            string subfunctionName = m.MenuFuncName;
            log.Debug("functionName:" + functionName + " >> " + m.MenuFuncName);
            SessionHelper sessionHelper = new SessionHelper();
            LogSystemVO logSystemVO = new LogSystemVO();
            logSystemVO.Fucntion = functionName;
            logSystemVO.SubFucntion = subfunctionName;
            sessionHelper.LogVO = logSystemVO;
        }
    }
}